package com.sphd.education.activitys;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.adapter.ClassifyAdapter;
import com.sphd.education.adapter.ClassifyRecyclerAdapter;
import com.sphd.education.adapter.InterestedLessonAdapter;
import com.sphd.education.info.ClassifyOne;
import com.sphd.education.info.ClassifyTwo;
import com.sphd.education.interfaces.RecyclerViewOnCheckChanged;
import com.sphd.education.interfaces.RecyclerViewOnClickListener;
import com.sphd.education.utils.ToastUtils;
import com.sphd.education.view.tagview.TagCloudView;

import org.litepal.crud.DataSupport;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
    /*
    * 添加兴趣课程的界面
    * */
public class AddInterestingActivity extends AppCompatActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.lesson_type_recyclerview)
    RecyclerView lessonTypeRecyclerview;
    @Bind(R.id.lesson_recyclerview)
    RecyclerView lessonRecyclerView;

    @Bind(R.id.interest_lesson)
    TextView interestLesson;
    @Bind(R.id.interest_lesson_tag_cloud_view)
    TagCloudView interestLessonTagCloudView;
    private List<ClassifyOne> classifyOneList;
    private ClassifyRecyclerAdapter classifyAdapter;

    private List<ClassifyTwo> classifyTwoList;
    private InterestedLessonAdapter classifyTwoAdapter;
    private LinearLayoutManager lessonLayoutManager;

    private ContentValues contentValues;
    private List<String> interestList;
    private List<ClassifyTwo> classifyTwoListChecked;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_interesting);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        toolbar.setTitle("选择兴趣课程");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(AddInterestingActivity.this,FinishMySelfActivity.class);
                intent.putExtra("checxedList", (Serializable) classifyTwoListChecked);
                setResult(1,intent);
                finish();
            }
        });
        contentValues = new ContentValues();
        interestList = new ArrayList<String>();
        classifyTwoListChecked = new ArrayList<ClassifyTwo>();
        classifyOneList = DataSupport.findAll(ClassifyOne.class);//左边类别的list
        classifyAdapter = new ClassifyRecyclerAdapter(this, classifyOneList, R.layout.layout_classify_item);
        lessonTypeRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        lessonTypeRecyclerview.setAdapter(classifyAdapter);
        classifyAdapter.setSelectedPosition(0);

        classifyTwoList = DataSupport.order("classifytwo_id asc").find(ClassifyTwo.class);//右边对应课程的list
        classifyTwoAdapter = new InterestedLessonAdapter(this, classifyTwoList, R.layout.item_interested_lesson_checkbox);
        lessonLayoutManager=new LinearLayoutManager(this);
        lessonRecyclerView.setLayoutManager(lessonLayoutManager);
        lessonRecyclerView.setAdapter(classifyTwoAdapter);

        classifyAdapter.setRecyclerViewOnClickListener(new RecyclerViewOnClickListener() {
            @Override
            public void onTouch(View view, int position) {
                classifyAdapter.setSelectedPosition(position);
                classifyAdapter.notifyDataSetChanged();
                for (int i = 0; i < classifyTwoList.size(); i++) {
                    if (classifyOneList.get(position).getSid() == classifyTwoList.get(i).getClassifyone_id()) {
                        lessonRecyclerView.smoothScrollToPosition(i);
                        break;
                    }
                }
            }
        });

        //右边的课程滑动监听是左边的课程类别与之对应显示
        lessonRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int firsPos = lessonLayoutManager.findFirstVisibleItemPosition();
                for (int i = 0; i < classifyOneList.size(); i++) {

                    if (classifyOneList.get(i).getSid() == classifyTwoList.get(firsPos).getClassifyone_id()) {
                        lessonTypeRecyclerview.scrollToPosition(i);
                        classifyAdapter.setSelectedPosition(i);
                        classifyAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
        classifyTwoAdapter.setRecyclerViewOnCheckChanged(new RecyclerViewOnCheckChanged() {
            @Override
            public void onCheckedChanged(CheckBox view, int position) {
                classifyTwoList.get(position).setLike_lesson(view.isChecked());
                ClassifyTwo classifyTwo = classifyTwoList.get(position);
                Log.e("checx",view.isChecked()+"");
                if (view.isChecked()){
                    if (classifyTwoListChecked.size()<=2){
                        classifyTwoListChecked.add(classifyTwo);
                    }else {
                        classifyTwoList.get(position).setLike_lesson(false);
                        classifyTwoAdapter.notifyDataSetChanged();
                        ToastUtils.MyToast(AddInterestingActivity.this,"选择，已超出三个");
                    }

                }else {
                    classifyTwoListChecked.remove(classifyTwo);
                }
                Log.e("checx.SIZE",classifyTwoListChecked.size()+"");

                if (classifyTwoListChecked.size() != 0) {
                        String lessonlikes = "";
                        interestList.clear();
                        for (int i = 0; i < classifyTwoListChecked.size(); i++) {
                            interestList.add(classifyTwoListChecked.get(i).getClassify());
                            lessonlikes = lessonlikes + classifyTwoListChecked.get(i).getClassify();
                        }
                        interestLessonTagCloudView.setTags(interestList);
//                    interestLesson.setText(lessonlikes);
                    } else {
                        interestList.clear();
                        interestLessonTagCloudView.setTags(interestList);
                        interestLesson.setText("");
                    }
            }
        });

    }
    @OnClick(R.id.clear_all_tag)
    public void setClearAllTagListener() {
        classifyTwoListChecked.clear();
        for (int i = 0; i < classifyTwoList.size(); i++) {
            classifyTwoList.get(i).setLike_lesson(false);
        }
        interestList.clear();
        interestLessonTagCloudView.setTags(interestList);
        interestLesson.setText("");
        classifyTwoAdapter.notifyDataSetChanged();
    }
}
